// $URL: http://mingw-lib.googlecode.com/svn/trunk/local/box/yt_dump/page/page2_list1_updated.js $
// $Rev: 767 $
// $Author: akio.miyoshi $
// $Date:: 2012-10-06 05:39:48 +0000#$

dbgmsg("BEGIN page2.list1.updated.js");

var g_record_height = 110;

var g_append =
"<table border=0><tr>\n"+
"<td><img width=90 height=70 /></td>\n"+
"<td>\n"+
"<div class='height_1em'><h_view_rank/> <h_rank_diff_mark/>(前回=<h_view_rank_old/>)<h_video_id/></div>\n"+
"<div class='height_1em'><h_title/></div>\n"+
"<div class='height_1em'><h_page/> <h_count/></div>"+
"<div class='height_1em' style='height:1.7em'>"+
"<button class=再生>再生</button>\n"+
"<button class=リセット>リセット</button>\n"+
"<button class=トップ>トップ</button>\n"+
"<h_author/>\n"+
"<button class=管理>管理</button>\n"+
"<button class=削除>削除</button>\n"+
"<button class=再生2>再生2</button>\n"+
"</div>"+
"<div class='height_1em'><h_pubdate/></div>"+
"</td>\n"+
"</tr></table>"
;

var g_preparing = 0;

function fn_prepare_h_record(h_append)
{
	if(h_append.data("prepared")) return;
	h_append.data("prepared", true);

	g_preparing++;

	var v_object = h_append.data("info");
	var i = v_object.i;

	dbgmsg("prepare: "+v_object.title);

	var v_append = $(g_append);

	var v_count;
    /*
	if(list1.sort_type.indexOf("favorite_") == 0)
	{
		v_count = "<span><%0>[<%L1> <%2> (+<%L3>)] <span style='color:red;'>再生回数=<%L4></span></span>";
		v_count = v_count
		.replace(/<%0>/g, "お気に入り")
		.replace(/<%L1>/g, fn_format_number(list1.get_field(i, "favorite_count")))
		.replace(/<%2>/g, "人")
		.replace(/<%L3>/g, fn_format_number(list1.get_field(i, "favorite_count_diff")))
		.replace(/<%L4>/g, fn_format_number(list1.get_field(i, "view_count")))
		;
	}
	else
    */
	{
		var v_count_elem = $(
			"<span>[<view_count/> 回 " +
			"(+<span class='view_diff_group'><view_count_diff/><view_ts_diff/></span>)] "+
			"<span class='favorite_group'>F=<favorite_count/>人(+<favorite_count_diff/>)</span>"+
			"</span>"
			);
		$("span.view_diff_group",     v_count_elem).css({color:"red"});
		$("span.favorite_group",      v_count_elem).css({color:"orange"});
		$("view_count",          v_count_elem).replaceWith(fn_format_number(v_object.view_count));
		$("view_count_diff",     v_count_elem).replaceWith(fn_format_number(v_object.view_count_diff));
		$("view_ts_diff",        v_count_elem).wrap("<a href='javascript:handle_hour_link("+v_object.hour+");return false;'></a>");
		$("view_ts_diff",        v_count_elem).replaceWith(v_object.view_ts_diff);
		$("favorite_count",      v_count_elem).replaceWith(fn_format_number(v_object.favorite_count));
		$("favorite_count_diff", v_count_elem).wrap("<span style='color:blue'></span>");
		$("favorite_count_diff", v_count_elem).replaceWith(fn_format_number(v_object.favorite_count_diff));
		////v_count = v_count_elem.get(0).outerHTML;
		v_count = v_count_elem;
	}
	var v_count_jq = $(v_count);
	//dbgmsg(v_count_jq.get(0));
	//dbgmsg(v_count_jq.get(0).outerHTML);

	//<table>
	//$("table", v_append).css({height:g_record_height+"px"});
	$("td", v_append).css({height:g_record_height+"px"}).attr("valign", "middle");
	//<h_video_id/>
	if($("h_video_id", v_append).text() == "")
	{
		$("h_video_id", v_append).text(v_object.id);
	}
	//<h_title/>
	if($("h_title", v_append).text() == "")
	{
		$("h_title", v_append).text(v_object.title);
	}
	//<h_author/>
	if($("h_author", v_append).text() == "")
	{
        if(v_object.author_id != "")
        {
            $("h_author", v_append)
            .text(v_object.author)
            .wrap("<a href='http://www.youtube.com/user/"+v_object.author_id+"'></a>");
        }
        else
        {
            $("h_author", v_append)
            .text(v_object.author)
            .wrap("<a href='http://www.youtube.com/user/"+v_object.author+"'></a>");
        }
	}
	//<h_pubdate/>
	//if($("h_pubdate", v_append).text() == "")
	{
		var v_date = new Date(v_object.pubdate);
		////var v_date_str = v_date.toUTCString();
		var v_dateFormat = new DateFormat("yyyy年MM月dd日 HH:mm:ss");
		var v_date_str = v_dateFormat.format(v_date); // Date(現在時刻)をStringに変換
		$("h_pubdate", v_append).text("公開日: "+v_date_str);
	}
	//<img>
	$("img", v_append)
			.data("video_id", v_object.id)
			//.data("icon_url", v_object.icon_url)
			.attr("src", v_object.icon_url)
			.click(function(){
		web1.submitIdToRelated($(this).data("video_id"));
	});
	/*
	$("img", v_append).oneTime(1000, "test", function(){
		$(this).attr("src", $(this).data("icon_url"));
	});
	*/
	//<h_view_rank/>
	$("h_view_rank", v_append)
			.text((i+1)+".第"+v_object.view_rank+"位")
			.wrap("<a href='#'></a>")
			.parent()
			.data("view_rank", v_object.view_rank)
			.click(function(){
		handle_rank_link($(this).data("view_rank"));
		return false;
	});
	//<h_rank_diff_mark/>
	$("h_rank_diff_mark", v_append).text(v_object.rank_diff_mark);
	//<h_view_rank_old/>
	$("h_view_rank_old", v_append).text("第"+v_object.view_rank_old+"位");
	//<h_page/>
	$("h_page", v_append)
			.text("["+v_object.page+"ページ("+v_object.pos+")]")
			.wrap("<a href='#'></a>")
			.parent()
			.data("page", v_object.page)
			.click(function(){
		handle_page_link($(this).data("page"));
		return false;
	});
	//<h_count/>
	$("h_count", v_append).html(v_count);
	//<button class=管理/>
	$("button.管理", v_append)
			////.text("管理")
			.data("page", v_object.page)
			.data("video_id", v_object.id)
			.click(function(){
		////web1.submitFavoritePage($(this).data("page"));
        ////xxx
        ////web1.submitFavoritePage2($(this).data("page"), $(this).data("video_id"));
        web1.submitIdToDownload($(this).data("video_id"));
	});
	//<button class=再生/>
	$("button.再生", v_append)
	.data("video_id", v_object.id)
	.click(function(){
		web1.submitIdToBrowse($(this).data("video_id"));
	});
	//<button class=再生2/>
	$("button.再生2", v_append)
	.data("video_id", v_object.id)
	.click(function(){
        ////fn_yt_player("#vlc_place", $(this).data("video_id"), "dummy");
        web1.submitIdToPlay2($(this).data("video_id"));
     });
	//<button class=リセット/>
	$("button.リセット", v_append)
			////.text("リセット")
			.data("i", v_object.i)
			.data("video_id", v_object.id)
			.click(function(){
		web1.submitIdToReset($(this).data("video_id"));
		$(this).attr("disabled", true);
		$("#record"+($(this).data("i")+1)).css({background:"cyan"});
	});
	//<button class=トップ/>
	$("button.トップ", v_append)
			////.text("トップ")
			.data("i", v_object.i)
			.data("video_id", v_object.id)
			.data("page", v_object.page)
			.click(function(){
        $(this).attr("disabled", true);
        var v_status = web1.submitIdToRaise($(this).data("video_id"));
		if(v_status == 0)
		{
			web1.submitIdToReset($(this).data("video_id"));
            //$(this).attr("disabled", true);
			$("#record"+($(this).data("i")+1)).css({background:"pink"});
		}
		else
		{
            $(this).attr("disabled", false);
            alert("トップに失敗: "+v_status);
			if(v_status == 1)
			{
				web1.submitFavoritePage2($(this).data("page"), $(this).data("video_id"));
				$("#record"+($(this).data("i")+1)).css({background:"orange"});
			}
			if(v_status == 2)
			{
				web1.submitIdToBrowse($(this).data("video_id"));
				$("#record"+($(this).data("i")+1)).css({background:"yellow"});
			}
		}
	});
	//<button class=削除/>
	$("button.削除", v_append)
			////.text("削除")
			.data("i", v_object.i)
			.data("video_id", v_object.id)
			.click(function(){
		var v_status = web1.submitIdToDelete($(this).data("video_id"));
		if(v_status == 200)
		{
			$(this).attr("disabled", true);
			$("#record"+($(this).data("i")+1)).remove();
			handle_redisplay();
		}
		else
		{
			alert("削除に失敗: "+v_status);
		}
	});
	//dbgmsg(v_append.get(0).outerHTML);
	h_append.html(v_append);
	/*
	v_append.hide();
	v_append.oneTime(1000, "test2", function(){
		$(this).show();
	});
	*/
	g_preparing--;
}
function fn_prepare_or_wait(h_append)
{
	dbgmsg("oneTime(test3)"+new Date());
	if(g_scrolling > 0 || g_preparing > 0)
	{
		//dbgmsg("postpone");
		h_append.oneTime(50, "test4", function(){
			fn_prepare_or_wait($(this));
		});
	}
	else
	{
		//dbgmsg("prepare");
		fn_prepare_h_record(h_append);
		//dbgmsg(h_append.get(0).outerHTML);
	}
}
list1.updated.connect(function(){
	dbgmsg(list1.size());
	$("div#mydiv").html("");
	for(i=0; i<list1.size(); i++)
	{
		if(i%50==0) dbgmsg("i="+i+"/"+list1.size());
		////dbgmsg(list1.json(i));

		var v_object = eval("("+list1.json(i)+")");
		v_object.i = i;
		if(v_object.deleted) continue;

		v_object.hour = v_object.view_ts_hour;
		v_object.hour = Math.ceil(v_object.hour);
		if(v_object.hour == 0) v_object.hour = 1;

		//dbgmsg("JSON.stringify(v_object)="+JSON.stringify(v_object));

		var v_append = $("<div />")
					   .css({height:g_record_height+"px", width:"100%", overflow:"hidden"})
					   //.css({border:"solid 1px green"})
					   ;
		v_append
				.data("prepared", false)
				.data("info", v_object)
				.attr("id", "record"+(i+1))
				.addClass("page")
				.addClass("page"+v_object.page)
				.addClass("rank"+v_object.view_rank)
				.addClass("hour"+v_object.hour)
				////.addClass("show_true")
				;

		if(false)
		{
			v_append.html(
					"<table border=0><tr>\n"+
					"<td><img width=90 height=70 /></td>\n"+
					"<td></td>\n"+
					"</tr></table>"
					);
		}
		else
		{
			v_append.html(
					g_append
					);
		}
		$("td", v_append).css({height:g_record_height+"px"}).attr("valign", "middle");
        ////////$("img", v_append).attr("src", v_object.icon_url);

		//if(i<10) fn_prepare_h_record(v_append);
		//if(i<10) $("<img />").attr("src", v_object.icon_url);
		//fn_prepare_h_record(v_append);

		if(list1.sort_type == "delta"
		   || list1.sort_type == "unit_delta"
		   || list1.sort_type == "total"
		   || list1.sort_type == "latest"
		   || list1.sort_type == "waiting")
		{
			//if(list1.get_field(i, "view_count_diff") == 0)
			if(v_object.view_count_diff == 0)
			{
				v_append.addClass("zero_delta");
			}
		}
		if(list1.sort_type == "favorite_delta" || list1.sort_type == "favorite_total")
		{
			//if(list1.get_field(i, "favorite_count_diff") == 0)
			if(v_object.favorite_count_diff == 0)
			{
				v_append.addClass("zero_delta");
			}
		}
		////dbgmsg(v_append.get(0).outerHTML);

		//v_append.oneTime(0, "test3", function(){ fn_prepare_or_wait($(this)); });

		$("div#mydiv").append(v_append);
	}
	handle_changes();
	web1.cmd_refresh_display();
	//setTimeout(handle_changes, 10);
	return true;
});
dbgmsg("END page2.list1.updated.js");
